package com.hmall.gateway.filter;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.OrderedGatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;

@Component
public class PrintAnyGatewayFilterFactory extends AbstractGatewayFilterFactory<PrintAnyGatewayFilterFactory.Config> {
    @Override
    public GatewayFilter apply(Config config) {
//        return new GatewayFilter() {
//            @Override
//            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//                //获取调取信息
//                ServerHttpRequest request = exchange.getRequest();
//                //
//                System.out.println(" PrintAnyGatewayFilterFactory  执行了");
//                return chain.filter(exchange);
//            }
//        };
        return new OrderedGatewayFilter(new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                // 获取配置的属性值
                String a = config.getA();
                String b = config.getB();
                String c = config.getC();
                System.out.println(" a = " + a);
                System.out.println(" b = " + b);
                System.out.println(" c = " + c);
                System.out.println(" PrintAnyGatewayFilterFactory  执行了");
                return chain.filter(exchange);
            }
        }, 100);
    }



    //接受参数的静态内部类
    @Data
    public static class Config{
        private String a;
        private  String b;
        private  String c;
    }
    //配置接受
//    @Override
//    public List<String> shardList() {
//        return List.of("a","b","c");
//    }


    @Override
    public List<String> shortcutFieldOrder() {

        return List.of("a","b","c");
    }

    public PrintAnyGatewayFilterFactory() {
        super(Config.class);
    }
}
